package code.db.executor;

import java.util.List;

import code.db.DBExecutor;
import code.db.mapper.ColumnRowMapper;
import code.db.mapper.TableRowMapper;
import code.domain.Column;
import code.domain.Table;

public class OracleExecutor extends DefaultDBExecutor implements DBExecutor{
	
	private final String getTableSQL = "select t.table_name, tc.comments" +
			"  from user_tables t" +
			" inner join user_tab_comments tc on t.table_name = tc.table_name" +
			" where t.table_name = upper(?)";
	private final String getColumnSQL = "select c.COLUMN_NAME, " +
			"cc.comments, c.DATA_TYPE, c.NULLABLE, c.data_default" +
			"  from user_tab_columns c" +
			" inner join user_col_comments cc on c.TABLE_NAME = cc.table_name" +
			"                                and c.COLUMN_NAME = cc.column_name" +
			" where c.Table_Name = upper(?)"; 
	
	public Table getTable(String tableName){
		Table table = (Table) dbTemplate.queryForObject(getTableSQL, new String[]{tableName}, new TableRowMapper());
		if(table != null){
			List<Column> columnList = getColumns(tableName);
			table.setColumnList(columnList);
		}
		return table;
	}
	
	@SuppressWarnings("unchecked")
	public List<Column> getColumns(String tableName){
		List<Column> columnList = dbTemplate.query(getColumnSQL, new String[]{tableName}, new ColumnRowMapper());
		return columnList;
	}
}
